import urllib
import xml.dom.minidom as minidom
import json


def __GetCityID(province, city, country):
    xmlPath = "Weather/City.xml"
    dom = minidom.parse(xmlPath)
    root = dom.documentElement
    provinces = root.getElementsByTagName("province")
    for item0 in provinces:
        if item0.getAttribute("name") == province:
            cities = item0.getElementsByTagName("city")
            for item1 in cities:
                if item1.getAttribute("name") == city:
                    counties = item1.getElementsByTagName("county")
                    for item2 in counties:
                        if item2.getAttribute("name") == country:
                            return item2.getAttribute("weatherCode")


def __GetAqiLevel(aqi):
    if aqi < 0:
        return "未获取"
    elif aqi <= 50:
        return "优"
    elif aqi <= 100:
        return "良"
    elif aqi <= 150:
        return "轻度污染"
    elif aqi <= 200:
        return "中度污染"
    else:
        return "重度污染"


def __GetWeatherInfo(province, city, country, typeNo):
    id = __GetCityID(province, city, country)
    if id == None:
        return "no city"
    else:
        infoDict = {}
        if typeNo == 0:
            url = "http://d1.weather.com.cn/sk_2d/" + str(id) + ".html?_=1527134891441"
            header = {
                "Accept": "* / *",
                "Accept-Language": "zh-CN,zh;q=0.9",
                "Connection": "keep-alive",
                "Host": "d1.weather.com.cn",
                "Referer": "http://www.weather.com.cn/weather1d/101210101.shtml",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
            }
            request = urllib.request.Request(url, headers=header)
            response = urllib.request.urlopen(request)
            dataBt = response.read()
            dataStr = dataBt.decode("utf-8")
            dataStr = '{' + dataStr.split('{')[1]
            dataDict = json.loads(dataStr)
            infoDict["城市"] = dataDict["cityname"]
            infoDict["代码"] = dataDict["city"]
            infoDict["时间"] = dataDict["date"] + " " + dataDict["time"]
            if dataDict["aqi"]=="":
                dataDict["aqi"]="-1"
            infoDict["信息"] = {
                "气温": dataDict["temp"] + "℃",
                "天气": dataDict["weather"],
                "湿度": dataDict["SD"],
                "风向": dataDict["WD"] + dataDict["WS"],
                "空气质量": dataDict["aqi"] + " " + __GetAqiLevel(int(dataDict["aqi"])),
                "降水量": dataDict["rain"] + "mm",
                "24小时降水量": dataDict["rain24h"] + "mm",
            }
            infoStr = json.dumps(infoDict, ensure_ascii=False)
            return infoStr
        elif type == 1:
            url = "http://u2.weather.com.cn/sk_2d/" + str(id) + ".html?_=1527134891441"
            header = {
                "Accept": "* / *",
                "Accept-Language": "zh-CN,zh;q=0.9",
                "Connection": "keep-alive",
                "Host": "d1.weather.com.cn",
                "Referer": "http://www.weather.com.cn/weather/101210101.shtml",
                "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36"
            }
            request = urllib.request.Request(url, headers=header)
            response = urllib.request.urlopen(request)
            dataBt = response.read()
            dataStr = dataBt.decode("utf-8")
            print(dataStr)


def GetWeather1D(province, city, country):
    data = __GetWeatherInfo(province, city, country, 0)
    return data


def GetWeather7D(province, city, country):
    # TODO
    return None
